﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Streambolics.Gui;

namespace Streambolics.Notes.Gui
{
    public partial class DocumentList : ListViewUserControl
    {
        //  TODO : Make this a descendant of ListViewUserControl
        //  TODO : Make this a descendant of StandardUserControl
        //  TODO : Move the _Loading/BeginUpdate/EndUpdate ni StandardUserControl
        //  TODO : Make _Loading trigger a change in the cursor

        public DocumentList ()
        {
            InitializeComponent ();
        }

        public void AddDocument (Document aDocument)
        {
            if (aDocument != null)
            {
                ListViewItem newItem = list.Items.Add (aDocument.ToString ());
                newItem.Tag = aDocument;
                newItem.SubItems.Add (aDocument.Type);
                newItem.SubItems.Add (aDocument.Form);
            }
        }

        public void AddDocuments (IEnumerable<Document> aDocuments)
        {
            if (aDocuments != null)
            {
                BeginUpdate ();
                try
                {
                    foreach (Document d in aDocuments)
                    {
                        AddDocument (d);
                    }
                }
                finally
                {
                    EndUpdate ();
                }
            }
        }
        public void SetDocuments (IEnumerable<Document> aDocuments)
        {
            BeginUpdate ();
            try
            {
                list.Items.Clear ();
                AddDocuments (aDocuments);
            }
            finally
            {
                EndUpdate ();
            }
        }

        public void SetDocuments (IHasDocuments aDocuments)
        {
            if (aDocuments != null)
            {
                SetDocuments (aDocuments.Documents);
            }
        }

        [Browsable (false)]
        public Document Selected
        {
            get
            {
                if (list.FocusedItem == null)
                {
                    return null;
                }
                else
                {
                    return (Document)(list.FocusedItem.Tag);
                }
            }
        }

        private void list_DoubleClick (object sender, EventArgs e)
        {
            PropertyDialog d = new PropertyDialog ();
            d.ShowObject (Selected);
            d.ShowDialog ();
        }
    }
}
